<script setup>
import { ref, onMounted } from 'vue'
import PageFullContainer from '@/components/pageFullContainer.vue'
import { useRouter } from 'vue-router'
const router = useRouter()

import {
  getHotPropertyService,
  deleteHotPropertyService,
  searchHotPropertyService
} from '@/apis/property'

// 存储热门房源信息
const hotProperty = ref([])

// 实现分页需要的参数
const pageSize = ref(5)
const page = ref(1)
const total = ref(null)

// 获取热门房源需要的参数
const hotItem = ref({
  pageSize: pageSize.value,
  page: page.value
})

// 获取热门房源信息
const getHotProperty = async () => {
  isLoading.value = true
  const res = await getHotPropertyService(hotItem.value)
  hotProperty.value = res.data.data.rows
  total.value = res.data.data.count
  isLoading.value = false
}

onMounted(() => {
  getHotProperty()
})

// 控制loading加载的开启和关闭
const isLoading = ref(false)

// 处理页码变化
const handleCurrentChange = (newPage) => {
  hotItem.value.page = newPage
  getHotProperty()
}

// 处理页面显示数据条数的变化
const handleSizeChange = (newSize) => {
  hotItem.value.pageSize = newSize
  getHotProperty()
}

// 编辑热门房源
const editorHotProperty = (row) => {
  router.push({
    path: '/property/publish',
    query: {
      id: row.id
    }
  })
}

// 移除热门房源
const deleteHotProperty = async (row) => {
  await ElMessageBox.confirm('你确定要移除该热门房源吗？', '温馨提示', {
    confirmButtonText: '确认',
    cancelButtonText: '取消',
    type: 'warning'
  })

  // 移除热门房源
  await deleteHotPropertyService(row.id)
  ElMessage.success('移除热门房源成功！')
  getHotProperty()
}

// 搜索框内输入的内容
const SearchInput = ref(null)

// 搜索功能
const handleSearch = async () => {
  const res = await searchHotPropertyService(SearchInput.value)
  hotProperty.value = res.data.data
}

// 重置功能
const handleReset = () => {
  SearchInput.value = null
  getHotProperty()
}
</script>

<template>
  <PageFullContainer title="热门房源">
    <template #extra>
      <div class="extra">
        <div class="search">
          <el-input
            v-model="SearchInput"
            style="width: 240px"
            placeholder="请输入搜索内容"
            @keyup.enter="handleSearch"
            clearable
          />
          <el-button type="primary" @click="handleSearch">
            <template #default>搜索</template>
            <template #icon>
              <el-icon><i-ep-Search></i-ep-Search></el-icon>
            </template>
          </el-button>
          <el-button type="info" @click="handleReset">
            <template #default>重置</template>
            <template #icon>
              <el-icon><i-ep-Refresh></i-ep-Refresh></el-icon>
            </template>
          </el-button>
        </div>
      </div>
    </template>

    <!-- 热门房源 -->
    <el-table
      :data="hotProperty"
      style="width: 100%"
      v-loading="isLoading"
      v-if="hotProperty.length"
    >
      <el-table-column prop="date" type="index" label="序号" width="60" />
      <el-table-column label="房源名称">
        <template #default="scope">
          <div class="ellipsis">
            {{ scope.row.title }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="图片">
        <template #default="scope">
          <div class="image">
            <img :src="scope.row.image_url.interior_img[0].url" alt="" />
          </div>
        </template>
      </el-table-column>
      <el-table-column label="房源地址">
        <template #default="scope">
          <div class="ellipsis">
            {{ scope.row.address }}
          </div>
        </template>
      </el-table-column>
      <el-table-column label="户型">
        <template #default="scope">
          {{ scope.row.room_count }}室{{ scope.row.livingroom_count }}厅{{
            scope.row.bathroom_count
          }}卫
        </template>
      </el-table-column>
      <el-table-column prop="rent_type" label="租赁类型" />
      <el-table-column prop="decorate_type" label="装修" />
      <el-table-column prop="room_direction" label="朝向" />
      <el-table-column label="面积">
        <template #default="scope">
          {{ Math.floor(scope.row.room_area) }}
        </template>
      </el-table-column>
      <el-table-column label="房源价格">
        <template #default="scope">
          {{ Math.floor(scope.row.price) }}
        </template>
      </el-table-column>
      <el-table-column label="操作" width="150">
        <template #default="{ row }">
          <el-button type="primary" circle @click="editorHotProperty(row)">
            <template #icon>
              <i-ep-Edit></i-ep-Edit>
            </template>
          </el-button>
          <el-button type="danger" circle @click="deleteHotProperty(row)">
            <template #icon>
              <i-ep-Delete></i-ep-Delete>
            </template>
          </el-button>
        </template>
      </el-table-column>
    </el-table>

    <el-empty v-else :image-size="200" />

    <template #footer>
      <!-- 分页区域 -->
      <el-pagination
        background
        layout="jumper,total,sizes,prev,pager,next"
        :page-sizes="[2, 3, 5, 10]"
        v-model:page-size="pageSize"
        v-model:current-page="page"
        @size-change="handleSizeChange"
        @current-change="handleCurrentChange"
        :total="total"
      ></el-pagination>
    </template>
  </PageFullContainer>
</template>

<style lang="scss" scoped>
.extra {
  width: 1250px;
  display: flex;
  justify-content: space-between;

  .search {
    button {
      margin: 0 10px;
    }
  }
}

.image {
  width: 100px;
  height: 75px;

  img {
    width: 100%;
    height: 100%;
  }
}
</style>
